package com.scm.web.controller.inventory;

import java.util.List;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.core.page.TableSupport;
import com.scm.persist.model.ScmInventoryStash;
import com.scm.service.IScmInventoryStashService;
import com.scm.web.controller.inventory.vo.StashVO;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.scm.common.annotation.Log;
import com.scm.common.enums.BusinessType;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.common.core.page.TableDataInfo;

/**
 * 库存-仓库信息Controller
 * 
 * @author reptilemanager
 * @date 2024-03-09
 */
@Controller
@RequestMapping("/scm/inventory/stash")
public class ScmInventoryStashController extends BaseController
{
    private String prefix = "inventory/stash";

    @Autowired
    private IScmInventoryStashService scmInventoryStashService;

    @RequiresPermissions("system:stash:view")
    @GetMapping()
    public String stash()
    {
        return prefix + "/stash";
    }

    /**
     * 查询库存-仓库信息列表
     */
    @RequiresPermissions("system:stash:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmInventoryStash scmInventoryStash)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;

        List<ScmInventoryStash> list = scmInventoryStashService.selectScmInventoryStashList(scmInventoryStash,start,pageDomain.getPageSize());
        return getDataTable(list);
    }

    /**
     * 导出库存-仓库信息列表
     */
    @RequiresPermissions("system:stash:export")
    @Log(title = "库存-仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmInventoryStash scmInventoryStash)
    {
        List<ScmInventoryStash> list = scmInventoryStashService.selectScmInventoryStashList(scmInventoryStash,0,10);
        ExcelUtil<ScmInventoryStash> util = new ExcelUtil<ScmInventoryStash>(ScmInventoryStash.class);
        return util.exportExcel(list, "库存-仓库信息数据");
    }

    @PostMapping("/queryAll")
    @ResponseBody
    public AjaxResult queryAll()
    {
        List<ScmInventoryStash> list = scmInventoryStashService.selectAllScmInventoryStashList();

        List<StashVO> stashVOList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(list)){
            for (ScmInventoryStash scmInventoryStash : list) {
                stashVOList.add(StashVO.builder().value(scmInventoryStash.getId()).text(scmInventoryStash.getName()).build());
            }
        }
        return AjaxResult.success(stashVOList);
    }


    /**
     * 新增库存-仓库信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存-仓库信息
     */
    @RequiresPermissions("system:stash:add")
    @Log(title = "库存-仓库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScmInventoryStash scmInventoryStash)
    {
        SysUser sysUser = getSysUser();
        scmInventoryStash.setCreateUser(String.valueOf(sysUser.getUserId()));
        scmInventoryStash.setCreateTime(System.currentTimeMillis());
        scmInventoryStash.setStatus("1");
        return toAjax(scmInventoryStashService.insertScmInventoryStash(scmInventoryStash));
    }

    /**
     * 修改库存-仓库信息
     */
    @RequiresPermissions("system:stash:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmInventoryStash scmInventoryStash = scmInventoryStashService.selectScmInventoryStashById(id);
        mmap.put("scmInventoryStash", scmInventoryStash);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存-仓库信息
     */
    @RequiresPermissions("system:stash:edit")
    @Log(title = "库存-仓库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScmInventoryStash scmInventoryStash)
    {
        return toAjax(scmInventoryStashService.updateScmInventoryStash(scmInventoryStash));
    }

    /**
     * 删除库存-仓库信息
     */
    @RequiresPermissions("system:stash:remove")
    @Log(title = "库存-仓库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scmInventoryStashService.deleteScmInventoryStashById(ids));
    }
}
